/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package trombinoscope.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import trombinoscope.modele.Database;

/**
 *
 * @author jef
 */
@WebServlet(name = "ServletAjouterPromo", urlPatterns = {"/ServletAjouterPromo"})
public class ServletAjouterPromo extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
  protected void processRequest (HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, ClassNotFoundException, SQLException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        
            String nom = request.getParameter("nom");
            String annee = request.getParameter("annee");
            String filiere = request.getParameter("filiere");            
         
            Connection con = null;
            PreparedStatement ps = null;
            ResultSet rs = null;
        
        try 
        {                            
            Class.forName("com.mysql.jdbc.Driver"); 
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/trombinoscope","root","");
           
            String sql = "INSERT INTO groupe(idGroupe, nomGroupe, annee, filiere) VALUES(NULL, ?, ?, ?)";
            ps = con.prepareStatement(sql);
            
            ps.setString(1, nom);
            ps.setString(2, annee);
            ps.setString(3, filiere);
          
            
            ps.executeUpdate();           
      
            
            out.println("<script type='text/javascript'> alert('La promotion a etait ajouté avec succes.') </script>");
            out.println("<script type='text/javascript'> document.location.href='./accueil.jsp' </script>");
            
            
        }
    
        finally 
        {
           
            Database.close(rs);
            Database.close(ps);
            Database.close(con);
        }   
    }
    
    
            
     
    
     @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
       try{      
            try {
                processRequest(request, response);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(ServletAjouter.class.getName()).log(Level.SEVERE, null, ex);
            }
            } catch (SQLException ex) {
                Logger.getLogger(ServletAjouter.class.getName()).log(Level.SEVERE, null, ex);
            }
      
    }    
   @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            try {
                processRequest(request, response);
            } catch (SQLException ex) {
                Logger.getLogger(ServletAjouter.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (ClassNotFoundException ex) {
          //  Logger.getLogger(ServletAjouter.class.getName()).log(Level.SEVERE, null, ex);
        }
    }    
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}   